package MyFragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.pavolibrary.commands.LedAPI;
import com.qb.devtest.R;

import utils.MyUtil;

public class FragmentLed8 extends Fragment {

    private LedAPI mLed8;

    private String serialPort = "/dev/ttyS9";  //定义串口号
    private int serialBaudrate = 2400;  //定义波特率
    private int serialFlag = 0;

    private Runnable runnable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_led8, container, false);

        initLed8();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 在这里初始化你的Fragment视图组件
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mLed8 != null) {
            mLed8.disconnect();
            mLed8 = null;
        }
    }

    public void initLed8() {
        mLed8 = new LedAPI(getActivity());
        connect();
//        setEnabledBtn(false);
    }

    /**
     * 连接
     */
    private void connect() {
        if (mLed8 != null) {
            mLed8.disconnect();
        }
        runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    if (mLed8.SUCCESS == mLed8.connect(serialPort, serialBaudrate, serialFlag)) {
//                    showHandler.sendEmptyMessage(0);
                        MyUtil.showLog("led8", serialPort);
                        mLed8.Selial_LoopTest();
                    } else {
//                    showHandler.sendEmptyMessage(1);
                        MyUtil.showLog("led8", "false");
                    }
                } catch (Exception e) {
                    MyUtil.showLog("led8 connect Exception", e.getMessage());
                }
            }
        };
        new Thread(runnable).start();
    }

}